package com.ruoyi.business.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.business.vo.TNewsInfoVo;
import com.ruoyi.business.service.ITNewsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 新闻动态Controller
 *
 * @author Tellsea
 * @date 2024-01-17
 */
@Api(tags = "新闻动态Controller")
@RestController
@RequestMapping("/business/newsInfo")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TNewsInfoController extends BaseController {

    private final ITNewsInfoService tNewsInfoService;

    @ApiOperation("查询新闻动态列表")
    @PreAuthorize("@ss.hasPermi('business:newsInfo:list')")
    @GetMapping("/list")
    public TableDataInfo<TNewsInfoVo> list(TNewsInfoVo entity) {
        return tNewsInfoService.queryList(entity);
    }

    @ApiOperation("查询新闻动态所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TNewsInfoVo entity) {
        return AjaxResult.success("查询成功", tNewsInfoService.queryAll(entity));
    }

    @ApiOperation("导出新闻动态列表")
    @PreAuthorize("@ss.hasPermi('business:newsInfo:export')")
    @Log(title = "新闻动态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TNewsInfoVo entity) {
        List<TNewsInfoVo> list = tNewsInfoService.queryAll(entity);
        ExcelUtil<TNewsInfoVo> util = new ExcelUtil<>(TNewsInfoVo.class);
        util.exportExcel(response, list, "新闻动态数据");
    }

    @ApiOperation("获取新闻动态详细信息")
    @PreAuthorize("@ss.hasPermi('business:newsInfo:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tNewsInfoService.queryById(id));
    }

    @ApiOperation("新增新闻动态")
    @PreAuthorize("@ss.hasPermi('business:newsInfo:add')")
    @Log(title = "新闻动态", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TNewsInfoVo entity) {
        return toAjax(tNewsInfoService.save(entity));
    }

    @ApiOperation("修改新闻动态")
    @PreAuthorize("@ss.hasPermi('business:newsInfo:edit')")
    @Log(title = "新闻动态", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TNewsInfoVo entity) {
        return toAjax(tNewsInfoService.updateById(entity));
    }

    @ApiOperation("删除新闻动态")
    @PreAuthorize("@ss.hasPermi('business:newsInfo:remove')")
    @Log(title = "新闻动态", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tNewsInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
